﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for Pager
/// </summary>
public class Pager
{
    public int CurrentPage { get; set; }
    public int PageSize { get; set; }
    public int TotalRecord { get; set; }
    public int SegmentSize { get; set; }
    public string Previous { set; get; }
    public string Next { set; get; }
    public string Type { set; get; }
    public Pager()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="CurrentPage">Current page</param>
    /// <param name="pageSize">No of records in a page</param>
    /// <param name="segmentSize">No of page in a segment</param>
    /// <param name="totalRecord">Total of record</param>
    public Pager(int currentPage, int pageSize, int segmentSize, int totalRecord)
    {
        this.CurrentPage = currentPage;
        this.PageSize = pageSize;
        this.SegmentSize = segmentSize;
        this.TotalRecord = totalRecord;
        this.Previous = "Pre";
        this.Next = "Next";
        this.Type = "AutoParking";
    }

    public string CreateNavigation()
    {
        string str = "";
        int start = 1;
        int end = 0;
        int k = TotalRecord / PageSize;
        //kiem tra co sang trang ko
        int div = TotalRecord % PageSize;
        if (div != 0)
        {
            k = k + 1;
        }
        else
        {
            k = TotalRecord / PageSize;
        }

        if (this.CurrentPage <= this.SegmentSize)
        {
            start = 1;
        }
        else if ((this.CurrentPage % this.SegmentSize) == 0)
        {
            start = this.CurrentPage - (this.SegmentSize - 1);
        }
        else
        {
            start = this.CurrentPage - (this.CurrentPage % this.SegmentSize) + 1;
        }
        end = start + (this.SegmentSize - 1);
        if (end > k)
        {
            end = k + 1;
        }
        // trang hien tai
        string pageaspx = "";
        string strQueryString = "";
        string pQueryPara = "";
        for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
        {
            if (HttpContext.Current.Request.QueryString.Keys[i] != "p")
            {
                strQueryString = strQueryString + "&";
                pQueryPara = HttpContext.Current.Request.QueryString.Keys[i];
                strQueryString = strQueryString + HttpContext.Current.Server.UrlEncode(pQueryPara) + "=";
                strQueryString = strQueryString + HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.QueryString[pQueryPara].ToString());

            }
        }
        if (strQueryString.Length != 0)
        {
            strQueryString = "?" + strQueryString.Substring(1) + "&";//            
        }
        else
        {
            strQueryString = "?";
        }
        pageaspx = HttpContext.Current.Request.CurrentExecutionFilePath + strQueryString;
        //phan trang
        if (TotalRecord > PageSize)
        {
            
            //dau
            if (this.CurrentPage != 1)
            {
                int l = 0;
                l = this.CurrentPage - 1;
                str += string.Format(" <a  href=\"{0}p={1}\">{2}</a>", pageaspx, l, Previous);
            }

            //giua
            int middle = end;
            if ((end % this.SegmentSize) == 0)
            {
                middle = end + 1;
            }

            if (this.Type.Equals("AutoParking"))//Auto
            {
                str += string.Format("&nbsp;<b>{0}/{1}</b>&nbsp;", CurrentPage, middle);
            }
            else
            {
                for (int i = start; i < middle; i++)
                {
                    if (i == this.CurrentPage)
                    {
                        str += " <b>[" + i.ToString() + "]</b>";

                    }
                    else
                    {
                        str += " <a href=" + pageaspx + "p=" + i.ToString();
                        str += " > " + i.ToString() + "</a>";
                    }
                }
            }
            //cuoi
            if (this.CurrentPage < k)
            {
                int l = 0;
                l = this.CurrentPage + 1;
                str += string.Format("<a  href=\"{0}p={1}\">{2}</a>", pageaspx, l, Next);
            }
        }
        else
        {
            str += "";
        }
        return str;
    }
}